package com.irs.util;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;

public class UpUtils {
    //IO
    public static void upfile(MultipartFile file, HttpServletRequest request) {

        //获取服务器地址
        String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/bgm/");
        System.out.println(realPath);
        //绝对路径
        String basePath="C:/Users/Administrator/IdeaProjects/video6/src/main/webapp/WEB-INF/bgm/";


        File file1 = new File(realPath);

        if (!file1.exists()) {
            //必需有realPath的/WEB-INF/bgm/地址才能生成目录
            //mkdirs与mkdir的区别  mkdirs(/temp/one/two/there);可以建立多个文件夹
            // mkdir(/temp/one/two/there);只能建立一个文件夹
            file1.mkdirs();
        }
        File file2 = new File(basePath);
        if(!file2.exists()) {
            file2.mkdirs();
        }

        String orgName = file.getOriginalFilename();
        try {
            //输出
            FileOutputStream fos = new FileOutputStream(realPath+orgName,true);
            FileOutputStream fos1 = new FileOutputStream(basePath+orgName,true);
            //打印
            fos.write(file.getBytes());
            fos1.write(file.getBytes());
            //刷新
            fos1.flush();
            fos.flush();
            //关闭
            fos1.close();
            fos.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
    }

}
